// video.js
var codec = require('../../utils/Codec.js')
var timeFun = require('../../utils/util.js')
var mVideos = new Array
var appSecret = "46a356d7997e9fe17f58989ada025121"
var typeId = 0
var shareTitle = "好看的小视频"
var sharePath = "/pages/video/video"
var lastPlayPosition = -1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "videos": mVideos,
    "isEnd": false,
    "userId": "",
    "pageNum": 0,
    "isLoading": false,
    "isFinished": false,
    "screenWidth": '375px',
    "play": false,
    "currentPos": lastPlayPosition
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    typeId = options.typeId
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "screenWidth": res.windowWidth + 'px'
        })
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
        console.log(res.SDKVersion)
      }
    })
  },
  onClickPlay: function (event) {
    this.setData({
      play: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getVideos(true)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: shareTitle,
      path: sharePath,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("end")
    this.setData({
      "isEnd": true
    })
    if (this.data.isLoading || this.data.isFinished) {
      return;
    }
    this.getVideos(false)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoading) {
      return;
    }
    this.getVideos(true)
  },
  onFavClick: function (event) {
    var pos = event.currentTarget.dataset.position
    var videoId = this.data.videos[pos].id
    console.log(videoId)
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoUser/fav/save.html',
      data: {
        userId: wx.getStorageSync("userId"),
        articleId: videoId
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '收藏成功',
          icon: 'success',
          duration: 1000
        })
      }, fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log(res)
      }
    })
  },
  onItemClick: function (event) {
    console.log(event)
    var position = event.currentTarget.dataset.position
    var objId = this.data.videos[position].id
    var userId = this.data.videos[position].userId
    wx.navigateTo({
      url: "../detail/detail?objId=" + objId + "&&userId=" + userId
    })
  },
  onPlay: function (event) {
    var objId = event.currentTarget.dataset.position
    console.log("onPlay:" + objId)
  },
  onEnd: function (event) {
    var objId = event.currentTarget.dataset.position
    console.log("onEnd:" + objId)
  },
  onPause: function (event) {
    var objId = event.currentTarget.dataset.position
    console.log("onPause:" + objId)
  },
  onUpdate: function (event) {
    var objId = event.currentTarget.dataset.position
    console.log("onUpdate:" + objId)
  },
  videoBindPlay: function (event) {
    var objId = event.currentTarget.dataset.position
    var videoContext;
    var mCurrentPos = event.currentTarget.dataset.position
    if (mCurrentPos != lastPlayPosition) {
      if (lastPlayPosition != -1) {
        videoContext = wx.createVideoContext('myVideo' + lastPlayPosition)
        videoContext.pause()
      }
      lastPlayPosition = mCurrentPos
      this.setData({
        "currentPos": lastPlayPosition
      })
    }
    var that = this
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoArticle/addPlayNum.html',
      data: {
        objId: objId
      },
      success: function (res) {

      }, fail: function (res) {

      }, complete: function (res) {

      }
    })
  },
  onShareClick: function (event) {
    var objId = event.currentTarget.dataset.objid
    wx.navigateTo({
      url: "../detail/detail?objId=" + objId + "&showMask=true"
    })
    // var that = this
    // wx.request({
    //   url: 'https://api.jiefu.tv/app2/api/videoArticle/addShareNum.html',
    //   data: {
    //     objId: objId
    //   },
    //   success: function (res) {

    //   }, fail: function (res) {

    //   }, complete: function (res) {

    //   }
    // })
    // var title = event.currentTarget.dataset.name
    // shareTitle = title
    // sharePath = "../detail/detail?objId=" + objId
    // if (wx.showShareMenu) {
    // wx.showShareMenu({
    //   withShareTicket: true
    // })
    // } else {
    //   // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
    //   wx.showModal({
    //     title: '提示',
    //     content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
    //   })
    // }
  },
  getVideos: function (isRefresh) {
    if (isRefresh) {
      this.setData({
        "pageNum": 0,
      })
    }
    var that = this
    if (wx.canIUse("wx.showLoading")) {
      wx.showLoading({
        title: 'Loading',
      })
    } else {
      wx.showNavigationBarLoading()
    }
    this.setData({
      "isLoading": true
    })
    wx.request({
      url: 'https://api.jiefu.tv/app2/api/videoArticle/list.html',
      method: 'GET',
      data: {
        "wxAccount": "30",
        "userId": wx.getStorageSync('userId'),
        "typeId": '',
        "tagId": "",
        "bisRecommend": false,
        "pageNum": that.data.pageNum,
        "pageSize": 10
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        var pageNum = that.data.pageNum + 1
        if (isRefresh) {
          mVideos = res.data.data
          for (let i = 0; i < mVideos.length; i++) {
            mVideos[i].time = timeFun.formatTimeToHuman(mVideos[i].createTime)
          }
          that.setData({
            "pageNum": pageNum,
            "videos": mVideos,
            "isFinihsed": false
          })
        } else {
          if (res.data.data.length == 0) {
            that.setData({
              "isFinished": true
            })
          } else {
            var data = res.data.data
            mVideos = mVideos.concat(res.data.data)
            for (let i = 0; i < mVideos.length; i++) {
              mVideos[i].time = timeFun.formatTimeToHuman(mVideos[i].createTime)
            }
            that.setData({
              "pageNum": pageNum,
              "videos": mVideos,
              "isEnd": false
            });
          }
        }
      },
      fail: function (res) {

      },
      complete: function (res) {
        if (wx.canIUse("wx.hideLoading")) {
          wx.hideLoading();
        } else {
          wx.hideNavigationBarLoading()
        }
        that.setData({
          "isLoading": false
        })
        if (isRefresh) {
          wx.stopPullDownRefresh();
        }
      }
    })
  }
})